﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using SpeakupBackend.Classes;
using System.Windows.Documents;
using System.Globalization;
using SpeakupBackend;
using System.ComponentModel;

namespace Frontend
{
    class ActionGridEntry
    {
        TextBlock ClassTimeTextBlock = new TextBlock();
        TextBlock ClassNameTextBlock = new TextBlock();
        TextBlock ClassStatusTextBlock = new TextBlock();
        TextBlock ClassActionTextBlock = new TextBlock();
        TextBlock OperationStatusTextBlock = new TextBlock();
        TextBlock OperationStatusDescrTextBlock = new TextBlock();
            
        public Border OperationStatusBorder = new Border();

        Class Class;

        DelegateProxy<PropertyChangedEventHandler> PropertyChangedProxy =
                new DelegateProxy<PropertyChangedEventHandler>();

        public ActionGridEntry(Class cl)
        {
            Class = cl;

            /*Class.PropertyChanged += PropertyChangedProxy.ProxyConsumer;
            PropertyChangedProxy.ProxySource += Class_PropertyChanged;*/

            SetupControls();
            UpdateControls();
        }

        void Class_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            UpdateControls();
        }

        void SetupControls()
        {
            var textBlocks = new List<TextBlock> { ClassTimeTextBlock, 
                                                    ClassNameTextBlock, 
                                                    ClassStatusTextBlock,
                                                    ClassActionTextBlock,
                                                    OperationStatusTextBlock,
                                                    OperationStatusDescrTextBlock};

            foreach (var txt in textBlocks)
            {
                txt.Margin = new Thickness(10, 5, 10, 5);
                txt.TextWrapping = TextWrapping.Wrap;
                txt.TextAlignment = TextAlignment.Center;
                txt.VerticalAlignment = VerticalAlignment.Center;
            }

            OperationStatusBorder.Child = OperationStatusTextBlock;
            OperationStatusBorder.Margin = new Thickness(0.75);
        }

        void UpdateControls()
        {
            //time
            ClassTimeTextBlock.Inlines.Clear();

            string date = Class.Time.Date.ToString("dd MMM yyyy", CultureInfo.InvariantCulture);
            string time = String.Format(@"{0:hh\:mm} - {1:hh\:mm}", Class.Time.Begin, Class.Time.End);

            ClassTimeTextBlock.Inlines.Add(date);
            ClassTimeTextBlock.Inlines.Add(new LineBreak());
            ClassTimeTextBlock.Inlines.Add(time);

            //name
            ClassNameTextBlock.Inlines.Clear();

            string name = String.Format("{0} {1}", Class.Name.Name, Class.Name.Kind);
            ClassNameTextBlock.Inlines.Add(name);

            //status
            ClassStatusTextBlock.Inlines.Clear();
            ClassStatusTextBlock.Inlines.Add(Class.CurrentStatus.ToString());

            //action
            ClassActionTextBlock.Inlines.Clear();
            string action = Class.Action.ToString();
            if (Class.Action == ClassAction.Reserve)
            {
                action = "Reserve";
            }
            else if (Class.Action == ClassAction.CancelReservation)
            {
                action = "Cancel";
            }
            ClassActionTextBlock.Inlines.Add(action);
        }

        public void SetOperationStatus(string status, string description, Brush bkColor)
        {
            OperationStatusTextBlock.Inlines.Clear();
            OperationStatusTextBlock.Inlines.Add(status);
            OperationStatusBorder.Background = bkColor;

            OperationStatusDescrTextBlock.Inlines.Clear();
            OperationStatusDescrTextBlock.Inlines.Add(description);
        }

        public void AddToGrid(Grid grid, int row)
        {
            Grid.SetRow(ClassTimeTextBlock, row);
            Grid.SetColumn(ClassTimeTextBlock, (int)ActionGridHeader.ClassTime);
            grid.Children.Add(ClassTimeTextBlock);

            Grid.SetRow(ClassNameTextBlock, row);
            Grid.SetColumn(ClassNameTextBlock, (int)ActionGridHeader.ClassName);
            grid.Children.Add(ClassNameTextBlock);

            Grid.SetRow(ClassStatusTextBlock, row);
            Grid.SetColumn(ClassStatusTextBlock, (int)ActionGridHeader.ClassStatus);
            grid.Children.Add(ClassStatusTextBlock);

            Grid.SetRow(ClassActionTextBlock, row);
            Grid.SetColumn(ClassActionTextBlock, (int)ActionGridHeader.ClassAction);
            grid.Children.Add(ClassActionTextBlock);

            Grid.SetRow(OperationStatusBorder, row);
            Grid.SetColumn(OperationStatusBorder, (int)ActionGridHeader.OperationStatus);
            grid.Children.Add(OperationStatusBorder);

            Grid.SetRow(OperationStatusDescrTextBlock, row);
            Grid.SetColumn(OperationStatusDescrTextBlock, (int)ActionGridHeader.OperationStatusDescription);
            grid.Children.Add(OperationStatusDescrTextBlock);
        }

        public void RemoveFromGrid(Grid grid)
        {
            grid.Children.Remove(ClassTimeTextBlock);
            grid.Children.Remove(ClassNameTextBlock);
            grid.Children.Remove(ClassStatusTextBlock);
            grid.Children.Remove(ClassActionTextBlock);
            grid.Children.Remove(OperationStatusBorder);
            grid.Children.Remove(OperationStatusDescrTextBlock);

        }
        
    }
}
